/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.StringTokenizer;

import org.apache.commons.lang.StringUtils;

public class FileHelper {
	/**
	 * 获取文件的创建时间 java无法直接获取文件时间 在windows下使用cmd获取
	 * 
	 * @param file
	 * @return
	 */
	private static DecimalFormat df = new DecimalFormat("#0.00");

	public static String getFileCreateDate(File file) {
		String createTime = null;
		try {
			Process process = Runtime.getRuntime().exec(
					"cmd.exe /c dir" + file.getAbsolutePath() + "/tc");
			BufferedReader buffer = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			for (int i = 0; i < 5; i++) {
				buffer.readLine();
			}
			String stuff = buffer.readLine();
			StringTokenizer st = new StringTokenizer(stuff);
			String dateC = st.nextToken();
			String time = st.nextToken();
			String datetime = dateC.concat(time);
			buffer.close();
			return datetime;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return createTime;
	}

	public static void copyTo(File srcFile, File destFile) throws IOException {
		FileInputStream fis = new FileInputStream(srcFile.getPath());
		FileOutputStream fos = new FileOutputStream(destFile.getPath());
		byte[] buff = new byte[1024];
		int readed = -1;
		while ((readed = fis.read(buff)) > 0)
			fos.write(buff, 0, readed);
		fis.close();
		fos.close();
	}

	public static String getFileExtendName(String fileName) {
		String ext = "";
		if (StringUtils.isBlank(fileName))
			return ext;
		if (fileName.lastIndexOf(".") == -1)
			return ext;
		ext = StringUtils.substringAfterLast(fileName, ".");
		return ext;
	}

	/**
	 * 获取一个文件夹或者文件的大小
	 * 
	 * @param f
	 * @return
	 * @throws Exception
	 */
	public static long getFileSize(File file) throws Exception {
		long size = 0;
		if (!file.isDirectory())
			return file.length();
		File flist[] = file.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}

	public static String getSuitableSize(long fileSize) {
		String size = "0";
		if (fileSize > (1024 * 1024 * 1024))
			size = (new BigDecimal(fileSize).divide(new BigDecimal(
					1024 * 1024 * 1024), 2, 1))
					+ "GB";
		else if (fileSize > (1024 * 1024))
			size = (new BigDecimal(fileSize).divide(
					new BigDecimal(1024 * 1024), 2, 1))
					+ "MB";
		else if (fileSize > (1024))
			size = (new BigDecimal(fileSize).divide(new BigDecimal(1024), 2, 1))
					+ "KB";
		return size;
	}

	public static String getFilePath(String filePath, String account,
			String defaultPath) {
		String path = "";
		if (StringUtils.isBlank(filePath))
			return path;
		int index = filePath.indexOf(defaultPath + "\\" + account);
		if (index > 0)
			path = filePath.substring(index);
		return path;
	}

	//	删除文件夹
	//	param folderPath 文件夹完整绝对路径

	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); //删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); //删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//	删除指定文件夹下所有文件
	//	param path 文件夹完整绝对路径
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);//再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}

	public static String getExtension(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');
			if ((i > -1) && (i < (filename.length() - 1))) {
				return filename.substring(i + 1);
			}
		}
		return "";
	}

}
